public class Solution617 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1==null && root2==null){
            return null;
        }
        int a=root1==null?0:root1.val;
        int b=root2==null?0:root2.val;
        TreeNode node=new TreeNode(a+b);
        if (root1!=null && root1.left!=null || root2!=null && root2.left!=null){
            node.left=mergeTrees(root1==null?null:root1.left,root2==null?null:root2.left);
        }
        if (root1!=null && root1.right!=null || root2!=null && root2.right!=null){
            node.right=mergeTrees(root1==null?null:root1.right,root2==null?null:root2.right);
        }
        return node;
    }

    public static void main(String[] args) {
        System.out.println(new Solution617().mergeTrees(new TreeNode(1,new TreeNode(3,new TreeNode(5),null),new TreeNode(2)),new TreeNode(2,new TreeNode(1,null,new TreeNode(4)),new TreeNode(3,null,new TreeNode(7)))).left.right.val);
    }
}
